package com.weitu.observation.observationMonthReport.controller;



import com.alibaba.fastjson.JSONArray;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.observation.observationMonthReport.entity.ObservationMonthRepData;
import com.weitu.observation.observationMonthReport.entity.ObservationMonthReport;
import com.weitu.observation.observationMonthReport.service.ObservationMonthReportService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/4/1.
 */
@Controller
@RequestMapping(value="observation/observationCardMonthReport")
public class ObservationMonthReportController extends BaseController {
    private static final String MODULE_BATH_PATH=BASE_JSP_PATH+"observation/observationMonthReport";

    private final ObservationMonthReportService observationMonthReportService;

    @Autowired
    public ObservationMonthReportController(ObservationMonthReportService observationMonthReportService){
        this.observationMonthReportService=observationMonthReportService;
    }

    /**
     * 调到月报列表页面
     * @return
     */
    @RequestMapping(value="/list",method = RequestMethod.GET)
    public String list(){
        return MODULE_BATH_PATH+"/list";
    }

    /**
     * 观察月报列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request){
        Map<String,Object> paramMap=new HashMap<String,Object>();
        putParametersIntoMap(paramMap);
        int count=this.observationMonthReportService.queryCount(paramMap);

        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,count);

        PageView pageView=dataGridPagination.getPageView();
        paramMap.put("pageView",pageView);

        List<ObservationMonthReport> list=this.observationMonthReportService.queryList(paramMap);
        return dataGridPagination.flushDataGridData(list);
    }

    /**
     * 部门下的关联数据
     * @param deptId
     * @param year
     * @param month
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/dataList",method = RequestMethod.POST)
    public List<ObservationMonthRepData> dataListAjax(String deptId,String year,String month){

       List<ObservationMonthRepData> repDataList= this.observationMonthReportService.queryDataListNext(deptId,year+"-"+month);

        return repDataList;
    }
    /**
     * 跳到添加页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView add(){
        ModelAndView modelAndView=new ModelAndView();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("userId",BaseController.getSessionUser().getId());
        map.put("userName",BaseController.getSessionUser().getName());
        map.put("userRealName",BaseController.getSessionUser().getRealName());
        map.put("deptId",BaseController.getSessionUser().getDepartment().getId());
        map.put("deptName",BaseController.getSessionUser().getDepartment().getName());
        map.put("addTime", DateTimeUtil.formatterDateTime());
        modelAndView.addObject("userMessage",map);
        modelAndView.setViewName(MODULE_BATH_PATH+"/add");
        return modelAndView;
    }
    @SystemLog(module = "观察月报",methods = "添加月报")
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public AjaxResult addAjax(ObservationMonthReport monthReport,String monthRepDataList){

        List<ObservationMonthRepData> monthRepDatas=JSONArray.parseArray(monthRepDataList,ObservationMonthRepData.class);
        String id=IDGenerator.getId();
        monthReport.setId(id);
        monthReport.setDelState(0);
        monthReport.setStatusName("登记");
        int sort=0;
        for(int i=0;i<monthRepDatas.size();i++){
            monthRepDatas.get(i).setReportId(id);
            monthRepDatas.get(i).setId(IDGenerator.getId());
            monthRepDatas.get(i).setSort(sort);
            sort++;
        }
        int count=this.observationMonthReportService.insertReportAndData(monthReport,monthRepDatas);
        if(count==1){
           return AjaxResult.success("保存成功");
        }else{
           return AjaxResult.fail("保存失败");
        }
    }

    /**
     * 跳到修改页面
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView edit(String id){
        ModelAndView modelAndView=new ModelAndView();
        ObservationMonthReport observationMonthReport=this.observationMonthReportService.queryById(id);
        modelAndView.addObject("monthReport",observationMonthReport);
        modelAndView.setViewName(MODULE_BATH_PATH+"/edit");
        if(observationMonthReport.getAddUserId().equals(BaseController.getSessionUser().getId())){
            modelAndView.addObject("auth","yes");
        }else{
            modelAndView.addObject("auth","no");
        }
        return modelAndView;
    }

    /**
     * 跳到详情页面
     * @return
     */
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public ModelAndView detail(String id){
        ModelAndView modelAndView=new ModelAndView();
        ObservationMonthReport observationMonthReport=this.observationMonthReportService.queryById(id);
        modelAndView.addObject("monthReport",observationMonthReport);
        modelAndView.setViewName(MODULE_BATH_PATH+"/edit");
        if(observationMonthReport.getAddUserId().equals(BaseController.getSessionUser().getId())){
            modelAndView.addObject("auth","no");
        }else{
            modelAndView.addObject("auth","no");
        }
        return modelAndView;
    }

    @SystemLog(module = "观察月报",methods = "编辑观察月报")
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public AjaxResult editAjax(ObservationMonthReport monthReport,String monthRepDataList){
        monthReport.setUpdateTime(DateTimeUtil.formatterDateTime());
        List<ObservationMonthRepData> monthRepDatas=JSONArray.parseArray(monthRepDataList,ObservationMonthRepData.class);
        int sort=0;
        for(int i=0;i<monthRepDatas.size();i++){
            monthRepDatas.get(i).setReportId(monthReport.getId());
            monthRepDatas.get(i).setId(IDGenerator.getId());
            monthRepDatas.get(i).setSort(sort);
            sort++;
        }
        int count=this.observationMonthReportService.updateMontReport(monthReport,monthRepDatas);
        if(count==1){
            return AjaxResult.success("编辑成功");
        }else{
            return AjaxResult.fail("编辑失败");
        }
    }
    /**
     * 修改页面内的页面
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/monthRepData",method = RequestMethod.POST)
    public List<ObservationMonthRepData> monthRepData(String reportId){
        List<ObservationMonthRepData> observationMonthRepDataList=this.observationMonthReportService.queryByReportId(reportId);
        return observationMonthRepDataList;
    }

    /**
     * 判断名字是否重复
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/judgement",method = RequestMethod.POST)
    public AjaxResult judgement(HttpServletRequest request){
        Map<String,Object> paramMap=new HashMap<String,Object>();
        putParametersIntoMap(paramMap);
        int count=this.observationMonthReportService.selectJudgement(paramMap);
        if(count>0){
            return AjaxResult.fail("名称重复");
        }else{
            return AjaxResult.success("可以保存");
        }
    }

    /**
     * 删除观察卡月报
     * @param id
     * @return
     */
    @SystemLog(module = "观察月报",methods = "删除观察月报")
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public AjaxResult delete(String id){
        int count =this.observationMonthReportService.deleteReport(id);
        if(count==1){
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.fail("删除失败");
        }
    }
}
